<?php
declare (strict_types=1);

namespace app\controller;

use app\model\Goal as GoalModel;
use app\model\Task as TaskModel;
use app\model\Habit as HabitModel;
use app\model\ProgressLog as ProgressLogModel;
use think\facade\Request;
use think\facade\Validate;

class Report
{
    // 生成用户报告
    public function user()
    {
        $data = Request::get();

        // 验证请求参数
        $validate = Validate::rule([
            'user_id' => 'require|integer',
            'start_date' => 'require|date',
            'end_date' => 'require|date',
        ]);

        if (!$validate->check($data)) {
            return json(['status' => 'error', 'message' => $validate->getError()]);
        }

        $userId = $data['user_id'];
        $startDate = $data['start_date'];
        $endDate = $data['end_date'];

        // 获取目标统计数据
        $goals = GoalModel::where('user_id', $userId)
            ->whereBetweenTime('created_at', $startDate, $endDate)
            ->select();

        // 获取任务统计数据
        $tasks = TaskModel::where('user_id', $userId)
            ->whereBetweenTime('created_at', $startDate, $endDate)
            ->select();

        // 获取习惯统计数据
        $habits = HabitModel::where('user_id', $userId)
            ->whereBetweenTime('created_at', $startDate, $endDate)
            ->select();

        // 汇总报告数据
        $report = [
            'goals' => $goals,
            'tasks' => $tasks,
            'habits' => $habits,
        ];

        return json(['status' => 'success', 'data' => $report]);
    }

    // 生成小组报告
    public function group()
    {
        $data = Request::get();

        // 验证请求参数
        $validate = Validate::rule([
            'group_id' => 'require|integer',
            'start_date' => 'require|date',
            'end_date' => 'require|date',
        ]);

        if (!$validate->check($data)) {
            return json(['status' => 'error', 'message' => $validate->getError()]);
        }

        $groupId = $data['group_id'];
        $startDate = $data['start_date'];
        $endDate = $data['end_date'];

        // 获取任务统计数据
        $tasks = TaskModel::whereIn('task_id', function ($query) use ($groupId) {
            $query->table('group_member')
                ->where('group_id', $groupId)
                ->field('task_id');
        })
            ->whereBetweenTime('created_at', $startDate, $endDate)
            ->select();

        // 获取进度统计数据
        $progressLogs = ProgressLogModel::whereIn('task_id', function ($query) use ($groupId) {
            $query->table('group_member')
                ->where('group_id', $groupId)
                ->field('task_id');
        })
            ->whereBetweenTime('date', $startDate, $endDate)
            ->select();

        // 获取习惯统计数据
        $habits = HabitModel::whereIn('habit_id', function ($query) use ($groupId) {
            $query->table('group_member')
                ->where('group_id', $groupId)
                ->field('habit_id');
        })
            ->whereBetweenTime('created_at', $startDate, $endDate)
            ->select();

        // 汇总报告数据
        $report = [
            'tasks' => $tasks,
            'progress_logs' => $progressLogs,
            'habits' => $habits,
        ];

        return json(['status' => 'success', 'data' => $report]);
    }
}
